
'''
政务物联网运维管理系统服务
'''
from pymongo import MongoClient
from pao import log
from gov.data_process.gov_netthing_operation_web.gov_net_thing_service import GovNetThingAppService
from flask import session


def register(jsonrpc, db_addr, db_port):
    netthing_app = GovNetThingAppService(db_addr, db_port)
    token = 'token'

    @jsonrpc.method('getdata.return_all_collection_data')
    def __return_all_collection_data_service(collectionname):
        nonlocal netthing_app
        # 返回指定collection的全表内容
        res = netthing_app.return_all_collection_data(collectionname)
        return res

    @jsonrpc.method('getdata.return_netthing_object')
    def __return_netthing_object_service(userid):
        nonlocal netthing_app
        # 返回指定userid绑定的所有物联网对象的信息
        res = netthing_app.return_netthing_object(userid)
        return res

    @jsonrpc.method('ILoginService.Login_login')
    def __Login_login_service(user_name, password, platform_type, platform_version, ip):
        nonlocal netthing_app
        # 登录确认，返回结果，并修改登录时间，增加登录记录
        # 登录失败返回FALSE，登录成功且首次登录返回2及用户id，登录成功非首次登录返回1及用户id
        app_type = '后台管理web'
        res = netthing_app.login_confirm(
            user_name, password, platform_type, platform_version, ip, app_type)
        # 设置session
        session[token] = res['user_id']
        return res

    @jsonrpc.method('IDataService.Data_monitor_stat_emerg')
    def __Data_monitor_stat_emerg_service():
        nonlocal netthing_app
        collection_name = 'PT_NETTHING_ACCIDENT_MONITOR'
        condition = {'EMERGENT_LEVEL': '重大事故'}  # 条件可待确认后修改
        res = netthing_app.stat_event_number(collection_name, condition)
        return res

    @jsonrpc.method('IDataService.Data_monitor_stat_processing')
    def __Data_monitor_stat_processing_service():
        nonlocal netthing_app
        collection_name = 'PT_NETTHING_ACCIDENT_MONITOR'
        condition = {'STATE': '处理中'}  # 条件可待确认后修改
        res = netthing_app.stat_event_number(collection_name, condition)
        return res

    @jsonrpc.method('IDataService.Data_monitor_stat_untreated')
    def __Data_monitor_stat_untreated_service():
        nonlocal netthing_app
        collection_name = 'PT_NETTHING_ACCIDENT_MONITOR'
        condition = {'STATE': '未处理'}  # 条件可待确认后修改
        res = netthing_app.stat_event_number(collection_name, condition)
        return res

    @jsonrpc.method('IDataService.Data_monitor_map')
    def __Data_monitor_map_service():
        nonlocal netthing_app
        res = netthing_app.query_Data_monitor_map()
        return res

    @jsonrpc.method('IDataService.Data_monitor_list')
    def __Data_monitor_list_service():
        nonlocal netthing_app
        res = netthing_app.query_Data_monitor_map()
        return res

    @jsonrpc.method('IDataService.Data_query_list')
    def __Data_query_list_service(condition, page, number, sort_key):
        # condition为字典格式{key:value,key:value},若无条件则传入{}
        # sort_key为字典格式{key:1}1代表正序，-1代表逆序，若无需排序则传入{}
        nonlocal netthing_app
        collection_name = 'PT_NETTHING_ACCIDENT_MONITOR'
        res = netthing_app.query_list(
            condition, collection_name, page, number, sort_key)
        return res

    @jsonrpc.method('IDeviceService.Device_query_list')
    def __Device_query_list_service(condition,page,number,sort_key):
        nonlocal netthing_app
        collection_name='PT_TASK_MANAGE'
        res= netthing_app.query_list(
            condition, collection_name, page, number, sort_key)
        return res

    @jsonrpc.method('IDeviceService.Device_detail_basic')
    def __Device_detail_basic_service(Rtu_id):
        nonlocal netthing_app
        collection_name='PT_NETTHING_OBJECT'
        condition={'RTU_ID':Rtu_id}
        res = netthing_app.query_basic_data(condition,collection_name)
        return res
    
    @jsonrpc.method('IDeviceService.Device_detail_install')
    def __Device_detail_basic_service():
        nonlocal netthing_app

        return res

    @jsonrpc.method('IDeviceService.Device_detail_install')
    def __Device_detail_basic_service():
        nonlocal netthing_app

        return res
    
        @jsonrpc.method('IDeviceService.Device_detail_install')
    def __Device_detail_basic_service():
        nonlocal netthing_app

        return res